package vn.aptech.scrumgroup.model;

import demo.axitdn.physic.Unit;
import vn.aptech.scrumgroup.util.DrawUtil;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;

public class BacklogModel extends AbstractBaseModel {

	private float left;
	private float top;
	private float width;
	private float height;
	private int lineCount;
	private String text;
	private String[] footerText;
	private int footerColor;
	private int lineColor;

	public BacklogModel(float left, float top, float width, float height,
			int lineCount, String text, String[] footerText, int backColor,
			int foreColor, int lineColor, int footerColor) {
		super(backColor, foreColor);
		this.footerColor = footerColor;
		this.lineColor = lineColor;
		this.left = left;
		this.top = top;
		this.width = width;
		this.height = height;
		this.lineCount = lineCount;
		this.text = text;
		this.footerText = footerText;
		init();
	}// end constructor

	private void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isPointOf(float x, float y) {
		// TODO Auto-generated method stub
		if (x > left && x < left + width && y > top && y < top + height) {
			return true;
		}// end if
		return false;
	}

	@Override
	public void changeColor(int color) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDraw(Canvas c) {
		// TODO Auto-generated method stub
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setStyle(Style.FILL_AND_STROKE);
		float oldSize = paint.getTextSize();
		paint.setColor(backColor);
		c.drawRect(left, top, left + width, top + height, paint);

		// draw lines and number
		paint.setStrokeWidth(0.8f);
		paint.setColor(foreColor);
		float d = height / (lineCount + 1);

		for (int i = 0; i <= lineCount; i++) {
			paint.setColor(lineColor);
			c.drawLine(left, top + d * i, left + width, top + d * i, paint);
			float x = left + 2;
			float y = top + d * i + d * 3 / 4;
			paint.setTextSize(oldSize / 2);
			paint.setColor(foreColor);
			c.drawText("" + (i + 1), x, y, paint);
		}
		paint.setTextSize(oldSize);

		// draw text
		paint.setAlpha(255);
		paint.setColor(foreColor);
		Rect rect = new Rect();
		paint.setTextSize(DrawUtil.calculateTextSize(text, width - 10 * Unit.i, paint, 3));
		paint.getTextBounds(text, 0, text.length(), rect);
		float x = (left + width / 2) - rect.width() / 2 + 2 * Unit.i;
		float y = top + height / 5;
		c.drawText(text, x, y, paint);
		paint.setTextSize(oldSize);

		// draw footerText
		paint.setStrokeWidth(1.0f);
		StringBuilder builder = new StringBuilder("");
		if (footerText.length == 2) {
			paint.setTextSize(DrawUtil.calculateTextSize(footerText, width, paint, 3));
			builder.append(footerText[0] + " ").append(footerText[1]);
			paint.setColor(footerColor);
			paint.getTextBounds(builder.toString(), 0, builder.toString()
					.length(), rect);
			if (rect.width() <= width) {
				c.drawText(builder.toString(), left + width / 2 - rect.width()
						/ 2, top + height + 1.5f * rect.height(), paint);
			} else {
				paint.getTextBounds(footerText[0], 0, footerText[0].length(),
						rect);
				c.drawText(footerText[0], left + width / 2 - rect.width() / 2,
						top + height + 1.5f * rect.height(), paint);
				paint.getTextBounds(footerText[1], 0, footerText[1].length(),
						rect);
				c.drawText(footerText[1], left + width / 2 - rect.width() / 2,
						top + height + 3.0f * rect.height(), paint);
			}// end else
		}// end if
	}

	@Override
	public void onUpdate(double t) {
		// TODO Auto-generated method stub

	}

}
